import Cookies from 'js-cookie'
import { getBrowserInterfaceSize } from '@/utils/mUtils'

const app = {
  state: {
    sidebar: {
      opened: !!+Cookies.get('sidebarStatus')
    },
    browser: {
      x: 0,
      y: 0,
      col: ''
    }
  },
  mutations: {
    TOGGLE_SIDEBAR: (state, isopened) => {
      if (isopened === state.sidebar.opened) {
        return
      }
      state.sidebar.opened = typeof (isopened) !== 'undefined' ? isopened : !state.sidebar.opened
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
    },
    RESIZE: (state) => {
      let page = getBrowserInterfaceSize();
      state.browser.x = page.pageWidth;
      state.browser.y = page.pageHeight;
      state.browser.col =
        state.browser.x >= 1920 ? 'xl' :
          state.browser.x >= 1200 ? 'lg' :
            state.browser.x >= 992 ? 'md' :
              state.browser.x >= 768 ? 'sm' : 'xs'
    }
  },
  actions: {
    ToggleSideBar: ({ commit }, isopened) => {
      commit('TOGGLE_SIDEBAR', isopened)
    },
    Resize: ({ commit }) => {
      commit('RESIZE')
      window.onresize = () => {
        return (() => {
          commit('RESIZE')
        })()
      }
    }
  }
}

export default app
